#!/usr/bin/perl

if($ARGV[0]){
  use Switch;
  switch($ARGV[1]){
    case /--stop*/ 
      {
       print "stoping the task $ARGV[0]!\n";
       &stop_task;
      }
    case /--start*/ 
      {
       print "starting the task $ARGV[0]!\n";
       &start_task;
      }
    case /--list*/ 
      {
       print "listing the tasks\n"; 
       &list_tasks;
      }
    else {exit(1);}
 
    }
  }
else
  {
    exit(1);
  }

sub stop_task{
  $lines=`crontab -l`;
  while($line=<$lines>)
    {
      open(TMP, ">>tmp.cron");
      if(/*$ARGV[0]*/){$line="#".$line;}
      print TMP $line;
    }
  if(-e "tmp.cron")
  {system("crontab tmp.cron");
   system("rm tmp.cron");
  }
  else
  {print "No task to stop!\n";}
}

sub start_task{
  $lines=`crontab -l`;
  while($line=<$lines>)
    {
      open(TMP, ">>tmp.cron");
      if(/*$ARGV[0]*/){$line=~s/#//g;}
      print TMP $line;
    }
  if(-e "tmp.cron")
  {system("crontab tmp.cron");
   system("rm tmp.cron");
  }
  else
  {print "No task to start!\n";}
}

sub list_tasks{
  $tasks=`crontab -l`;
  print "$tasks\n";
}
